class Solution
{
    int n = 0, m = 0;
public:
    void solve(vector<vector<char>>& board)
    {
        n = board.size();
        m = board[0].size();

        for (int i = 0; i < n; ++i)
        {
            if (board[i][0] == 'O')
                dfs(board, i, 0);
            if (board[i][m - 1] == 'O')
                dfs(board, i, m - 1);
        }
        for (int j = 0; j < m; ++j)
        {
            if (board[0][j] == 'O')
                dfs(board, 0, j);
            if (board[n - 1][j] == 'O')
                dfs(board, n - 1, j);
        }

        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < m; ++j)
            {
                if (board[i][j] == 'O')
                    board[i][j] = 'X';
                if (board[i][j] == '.')
                    board[i][j] = 'O';
            }
        }
    }

    void dfs(vector<vector<char>>& board, int row, int col)
    {
        board[row][col] = '.';
        if (row - 1 >= 0 && board[row - 1][col] == 'O')
        {
            dfs(board, row - 1, col);
        }
        if (row + 1 < n && board[row + 1][col] == 'O')
        {
            dfs(board, row + 1, col);
        }
        if (col - 1 >= 0 && board[row][col - 1] == 'O')
        {
            dfs(board, row, col - 1);
        }
        if (col + 1 < m && board[row][col + 1] == 'O')
        {
            dfs(board, row, col + 1);
        }
    }

};
